import com.sun.corba.se.spi.ior.MakeImmutable;

public class StackSolution {
	NodeStack stack;
	boolean[][][] booleanArray;

	public StackSolution(){
		stack = new NodeStack();
		booleanArray = new boolean[10][][];
	}
	public NodeStack getStack() {
		return stack;
	}
	public void setStack(NodeStack stack) {
		this.stack = stack;
	}
	public boolean[][][] getBooleanArray() {
		return booleanArray;
	}
	public void setBooleanArray(boolean[][][] booleanArray) {
		this.booleanArray = booleanArray;
	}
	public boolean[][][] creatBoolean(Level[] levels) {
		boolean[][][] allLevels = new boolean[10][][];
		for (int l = 0; l < levels.length; l++) {
			if (levels[l] != null) {
				char[][] currentChar = levels[l].getLevel();
				boolean[][] currentBoolean = new boolean[currentChar.length][currentChar.length];
				for (int i = 0; i < currentChar.length; i++) {
					for (int j = 0; j < currentChar.length; j++) {
						if (currentChar[i][j] != 'x') {
							currentBoolean[i][j] = true;
						}
					}
				}
				allLevels[l] = currentBoolean;
			}
		}
		booleanArray = allLevels;
		return allLevels;
	}

	public void solveStack(Level[] levels, TermS start) {
		boolean[][][] validation = creatBoolean(levels);
		stack.push(start);
		validation[start.getLevel()][start.getPositionI()][start.getPositionJ()] = false;
		TermS term = start;
		while (!stack.isEmpty()
				
				&& levels[term.getLevel()].getLevel()[term.getPositionI()][term
						.getPositionJ()] != 'B'
				&& levels[term.getLevel()].getLevel()[term.getPositionI()][term
						.getPositionJ()] != 'V') 
		{
		System.out.println(stack.size());
			int i =term.getPositionI();
			int j = term.getPositionJ();
			if(i-1>=0&&j-1>=0&&validation[term.getLevel()][i-1][j-1]){
				validation[term.getLevel()][i-1][j-1]=false;
				TermS newTerm =new TermS(i-1,j-1);
				newTerm.setLevel(term.getLevel());
				stack.push(newTerm);
				term=newTerm;
			}else if(i-1>=0&&validation[term.getLevel()][i-1][j]){
				validation[term.getLevel()][i-1][j]=false;
				TermS newTerm =new TermS(i-1,j);
				newTerm.setLevel(term.getLevel());
				stack.push(newTerm);
				term=newTerm;
			}
			else if(i+1<validation[term.getLevel()].length&&j+1<validation[term.getLevel()].length&&validation[term.getLevel()][i+1][j+1]){
				validation[term.getLevel()][i+1][j+1]=false;
				TermS newTerm =new TermS(i+1,j);
				newTerm.setLevel(term.getLevel());
				stack.push(newTerm);
				term=newTerm;
			}
			else if(j+1<validation[term.getLevel()].length&&validation[term.getLevel()][i][j+1]){
				validation[term.getLevel()][i][j+1]=false;
				TermS newTerm =new TermS(i,j+1);
				newTerm.setLevel(term.getLevel());
				stack.push(newTerm);
				term=newTerm;
			}
			else if(i-1>=0&&validation[term.getLevel()][i-1][j]){
				validation[term.getLevel()][i-1][j+1]=false;
				TermS newTerm =new TermS(i-1,j+1);
				newTerm.setLevel(term.getLevel());
				stack.push(newTerm);
				term=newTerm;
			}
			else if(i+1<validation[term.getLevel()].length&&validation[term.getLevel()][i+1][j]){
				validation[term.getLevel()][i+1][j]=false;
				TermS newTerm =new TermS(i+1,j);
				newTerm.setLevel(term.getLevel());
				stack.push(newTerm);
				term=newTerm;
			}
			else if(i+1<validation[term.getLevel()].length&&j-1>=0&&validation[term.getLevel()][i+1][j-1]){
				validation[term.getLevel()][i+1][j-1]=false;
				TermS newTerm =new TermS(i+1,j-1);
				newTerm.setLevel(term.getLevel());
				stack.push(newTerm);
				term=newTerm;
			}
			else if(j-1>=0&&validation[term.getLevel()][i][j-1]){
				validation[term.getLevel()][i][j-1]=false;
				TermS newTerm =new TermS(i,j-1);
				newTerm.setLevel(term.getLevel());
				stack.push(newTerm);
				term=newTerm;
			}else{
				boolean enter=true;
				try{
					int stairs = Integer.parseInt(levels[term.getLevel()].getLevel()[term.getPositionI()][term
								.getPositionJ()]+"");
					if(levels[stairs]!=null){
						String sta=levels[stairs].getStairs()[term.getLevel()];
						int posI=Integer.parseInt(sta.substring(0,sta.indexOf(' ')));
						int posJ=Integer.parseInt(sta.substring(sta.indexOf(' ')+1,sta.length()));
						validation[stairs][posI][posJ]=false;
						TermS newTerm = new TermS(posI,posJ);
						newTerm.setLevel(stairs);
						stack.push(newTerm);
						term=(TermS) stack.top();
					}
				}catch(Exception e){
					enter=false;
				}
				if(!enter){
				term=(TermS) stack.pop();	
				}
			}
			
		}
	}
	public static void main(String []args){
		GenerateLevel levels = new GenerateLevel("C:\\My Documents\\map.txt");
		levels.checkStairs();
		Level[] lev = levels.getLevels();
		StackSolution st = new StackSolution();
		TermS start= new TermS(3,36);
		start.setLevel(6);
		st.solveStack(lev,start );
		NodeStack sc = st.getStack();
		while(!sc.isEmpty()){
			TermS term =(TermS) sc.pop();
			System.out.println(term.getPositionI()+"  "+term.getPositionJ());
		}
		
	}
}
